const express = require('express')
const md5 = require('md5')

const router = express.Router()
const { addAdmin, findAdmins } = require('../db/crud/adminFn')

router.post('/addadmin', async (req, res) => {
  const { username, password } = req.fields
  try {
    await addAdmin({ username, password: md5(password) })
    res.send({
      success: true,
      msg: '管理员添加成功',
    })
  } catch (error) {
    res.send({
      success: false,
      msg: '管理员添加失败',
      error,
    })
  }
})
router.get('/findadmins', async (req, res) => {
  // setTimeout(async () => {
  try {
    const result = await findAdmins()
    res.send({
      success: true,
      data: result,
    })
  } catch (error) {
    res.send({
      success: false,
      msg: '网络错误,请稍后再试',
    })
  }
  // }, 2000)
})

module.exports = router
